<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";

require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
        // console.log(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      // console.log(this.$el);
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData, true);
    },
    setOptions({
      assayOrderWeekCount,
      checkOrderWeekCount,
      serviceOrderCount,
      myTime,
    } = {}) {
      // console.log(assayOrderWeekCount, checkOrderWeekCount,serviceOrderCount,myTime)

      this.chart.setOption({
        xAxis: {
          data: myTime,
          boundaryGap: false,
          axisLabel: {
            rotate: 45, // 标签倾斜 45 度
            interval: 0, // 显示所有标签
            fontSize: 10, // 缩小字体
          },
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 50, // 左侧留出更多空间
          right: 50, // 右侧留出更多空间
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: ["注册用户", "实名认证"],
        },
        series: [
          {
            name: "注册用户",
            itemStyle: {
              normal: {
                color: "#67c23a",
                lineStyle: {
                  color: "#67c23a",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: assayOrderWeekCount,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "实名认证",
            itemStyle: {
              normal: {
                color: "#409eff",
                lineStyle: {
                  color: "#409eff",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: checkOrderWeekCount,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ],
        dataZoom: [
          {
            type: "slider", // 滑动条
            xAxisIndex: 0, // 控制第一个 X 轴
            start: 0, // 初始显示范围
            end: 50, // 初始显示范围
          },
          {
            type: "inside", // 内置缩放
            xAxisIndex: 0,
          },
        ],
      },true);
    },
  },
};
</script>
